//
//  YXYRetweetView.swift
//  SinaWeibo
//
//  Created by 袁向阳 on 16/6/29.
//  Copyright © 2016年 YXY.cn. All rights reserved.
//

import UIKit
import SnapKit

class YXYRetweetView: UIView {
    
    // 底部的约束
    var retweetViewBottomConstraint: Constraint?
    
    // 供外界赋值
    var status: YXYStatus? {
        didSet {
            contentLabel.text = status?.retweeted_status?.text
            
            // 卸载约束
            retweetViewBottomConstraint?.uninstall()
            
            // 判断图片视图是否显示，并且赋值
            if status?.retweeted_status?.pic_urls?.count > 0 {
                pictureView.hidden = false
                pictureView.pic_urls = status?.retweeted_status?.pic_urls
                self.snp_updateConstraints(closure: { (make) -> Void in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(constantMargin).constraint
                })
            }else {
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                 retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(constantMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1.0)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 创建子控件
    private func setUpUI () {
        
        addSubview(contentLabel)
        addSubview(pictureView)
        
        addSubViewsConstraint()
    }
    
    // MARK: - 添加子控件约束
    private func addSubViewsConstraint () {
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(constantMargin)
            make.left.equalTo(self).offset(constantMargin)
            make.right.equalTo(self).offset(-constantMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(constantMargin)
            make.left.equalTo(self).offset(constantMargin)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(constantMargin).constraint
        }
        
    }
    
    // 内容label
    lazy var contentLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.darkGrayColor()
        lab.font = UIFont.systemFontOfSize(14)
        lab.numberOfLines = 0
        return lab
    }()
    // 图片视图
    lazy var pictureView: YXYPictureView = YXYPictureView()
}
